#!/bin/bash

# 脚本名称：add_current_dir_to_bashrc.sh
# 功能：将当前目录添加到.bashrc的PATH环境变量中

# 检查.bashrc文件是否存在
if [ ! -f ~/.bashrc ]; then
    echo "错误：未找到 ~/.bashrc 文件"
    exit 1
fi

# 获取当前目录的绝对路径
CURRENT_DIR=$(pwd)

# 检查是否已经存在于PATH中
if grep -q "PATH=.*$CURRENT_DIR" ~/.bashrc; then
    echo "提示：当前目录已经存在于 ~/.bashrc 的PATH中"
    exit 0
fi

# 备份原有的.bashrc文件
cp ~/.bashrc ~/.bashrc.bak
echo "已创建备份文件：~/.bashrc.bak"

# 添加到PATH环境变量
echo "" >> ~/.bashrc
echo "# 由add_current_dir_to_bashrc.sh脚本添加 $(date)" >> ~/.bashrc
echo "export PATH=\"\$PATH:$CURRENT_DIR\"" >> ~/.bashrc

echo "成功将当前目录 ($CURRENT_DIR) 添加到 ~/.bashrc 的PATH环境变量中"

# 提示用户需要重新加载配置
echo ""
echo "要使更改立即生效，请执行以下命令之一："
echo "1. 重新打开终端"
echo "2. 执行: source ~/.bashrc"
echo "3. 执行: . ~/.bashrc"

exit 0